package com.phoenix.callAnalysis;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Closeable;

import com.phoenix.callAnalysis.models.Constants;

/**
 * Displays an EULA ("End User License Agreement") that the user has to accept
 * before using the application.
 */
class Eula {
	private static final String TAG = Eula.class.getSimpleName();

	/**
	 * Displays the EULA if necessary.
	 * @return true: accepted before; false: no accepted before
	 */
	static boolean show(final Activity activity) {
		final SharedPreferences mPreferences = CallAnalysisApplication.mPreferences;

		// to test.
//		mPreferences.edit()
//				.putBoolean(Constants.CALL_PREFERENCES_EULA_ACCEPTED, false)
//				.commit();

		if (!mPreferences.getBoolean(Constants.CALL_PREFERENCES_EULA_ACCEPTED,
				false)) {
			Log.d(TAG, "EULA show...");
			final AlertDialog.Builder builder = new AlertDialog.Builder(
					activity);
			builder.setTitle(R.string.eula_title);
			builder.setCancelable(true);
			builder.setPositiveButton(R.string.eula_accept,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							accept(mPreferences);
						}
					});
			builder.setNegativeButton(R.string.eula_not_accept,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							refuse(activity);
						}
					});
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
				public void onCancel(DialogInterface dialog) {
					refuse(activity);
				}
			});
			String language = mPreferences.getString(
					Constants.CALL_PREFERENCES_LANGUAGE,
					Constants.CALL_PREFERENCES_LANGUAGE_DEFAULT);
			builder.setMessage(readEula(activity, language));
			builder.create().show();
			return false;
		}
		return true;
	}

	private static void accept(SharedPreferences preferences) {
		CallAnalysisApplication.myDB.createTable();
		preferences.edit()
				.putBoolean(Constants.CALL_PREFERENCES_EULA_ACCEPTED, true)
				.commit();
		Log.v(TAG, "EULA accept!");
	}

	private static void refuse(Activity activity) {
		Log.v(TAG, "EULA refuse!");
		activity.finish();
	}

	private static CharSequence readEula(Activity activity, String language) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(activity.getAssets()
					.open(language + Constants.CALL_EULA_ASSETS)));
			String line;
			StringBuilder buffer = new StringBuilder();
			while ((line = in.readLine()) != null)
				buffer.append(line).append('\n');
			return buffer;
		} catch (IOException e) {
			return "";
		} finally {
			closeStream(in);
		}
	}

	/**
	 * Closes the specified stream.
	 */
	private static void closeStream(Closeable stream) {
		if (stream != null) {
			try {
				stream.close();
			} catch (IOException e) {
			}
		}
	}
}